//
//  AppDelegate.swift
//  01-界面搭建
//
//  Created by Apple on 15/11/3.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        //创建主窗口
      //  print(UserAccountViewModel.shardViewModel)
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        // window?.rootViewController = New_FeatureCollectionViewController()
        window?.rootViewController = defaultViewController
        window?.makeKeyAndVisible()
        
        print(isVersion)//这个也要走一遍判断，进行存储版本号
        setupAPPearance()
        //监听通知
        NSNotificationCenter.defaultCenter().addObserverForName(
            WBSwitchRootViewControllerNotification,    // 通知名称，通知中心用来识别通知的
            object: nil,                               // 发送通知的对象，如果为nil，监听任何对象
            queue: nil)                               // nil，主线程
            { (NSNotification) -> Void in
            //print(NSNotification)//打印 NSConcreteNotification 0x7fb60be0ba50 {name = WBSwitchRootViewControllerNotification}

               //切换控制器
                let vc = NSNotification.object != nil ? WelocmViewController() : MainViewController()
              self.window?.rootViewController = vc
        }
        return true
    }
    //注销通知
    deinit {
    
    NSNotificationCenter.defaultCenter().removeObserver(self, name: WBSwitchRootViewControllerNotification, object: nil)
    
    }
    //修改全局外观
    private func setupAPPearance() {
        
        //在控件创建之前设置，全局有效
        UINavigationBar.appearance().tintColor = WBAppearanceTintColor
        UITabBar.appearance().tintColor = WBAppearanceTintColor
        
    }
    
}
//MARK: - 界面切换代码
extension AppDelegate {
    //启动的根视图控制器
    private var defaultViewController:UIViewController {
        //判断是否登录
        if UserAccountViewModel.shardViewModel.userLogin {
            
            return isVersion ? New_FeatureCollectionViewController() : WelocmViewController()
            
        }
        return New_FeatureCollectionViewController()
    }
    
    //判断版本号  CFBundleVersion
    private var isVersion:Bool{
        //获取当前版本
        let NewVersion = NSBundle.mainBundle().infoDictionary!["CFBundleVersion"] as! String
        let version = Double(NewVersion)!
        print("当前版本号是:\(version)")
        //之前版本号 存储在本地的版本号
        let sanBox = "sanBox"
        
        let OldVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sanBox)
        print("之前版本号是:\(OldVersion)")
        //存储当前版本号
        NSUserDefaults.standardUserDefaults().setObject(version, forKey: sanBox)
        //返回比较
        return version > OldVersion
    }
    
}

